package Lesson19Notepad;

public class SimpleNotepad implements INotepad 
{
	/*
	private String title;
	private String page;
	*/
	private Page[] notepad;
	
	public SimpleNotepad( int NumberOfPages) 
	{		
		if (NumberOfPages > 0)
		{
			this.notepad = new Page[NumberOfPages];
			for (int i = 0; i < notepad.length; i++) 
			{
				notepad[i] = new Page(String.valueOf(i + 1));
			}
		} 
		else 
		{
			System.out.println("Wrong input");
		}		
	}
	
	
	@Override
	public void addText(int page, String text) 
	{
		if (checkPage(page, notepad.length - 1))
		{
		notepad[page].addText(text);
		}
		
	}

	@Override
	public void addNewText(int page, String newText) 
	{
		if(notepad[page] != null)
		{
		notepad[page].delText();
		notepad[page].addText(newText);
		}
	}

	@Override
	public void delText(Page selectedPage) 
	{
		if (selectedPage != null)
		{
			selectedPage.delText();
		}
		else 
		{
			System.out.println("Wrong page!");
		}
		
		
	}

	@Override
	public void printPages() 
	{
		for(int i = 0; i < notepad.length; i++)
		{
			if(notepad[i] != null)
			{
				notepad[i].readPage();
			}
		}	
		
	}
	
	
	private boolean checkPage(int pageNumber, int length)
	{
		if (pageNumber > length)
		{
			return false;
		}
		else
		{
			return true;
		}
	}


	@Override
	public void searchWord(String word) 
	{
		
		for(int i = 0; i < notepad.length; i++)
		{
			notepad[i].searchWord(word);
			
		}	
	}


	@Override
	public void printAllPagesWithDigits() 
	{
		for(int i = 0; i < notepad.length; i++)
		{
			if(notepad[i].containsDigits() == true)
			{
				System.out.println("Page: " + i );
			}
			
		}	
		
	}
	
	
	
	
}
